package com.etc.controller;

import com.etc.entity.User;
import com.etc.entity.UserRecordFinall;
import com.etc.service.UserRecordService;
import com.etc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by dzq on 2019/5/24.
 */
@RestController
public class UserRecordController {
    @Autowired
    private UserRecordService userRecordService;
    @Autowired
    private UserService userService;


    /**
     * 直接支付成功后个人中心的显示
     *
     * @param session
     * @return
     */
    @GetMapping(value = "personal1")
    public ModelAndView UserRecordDisplay(HttpSession session, @CookieValue("userid") String useridcookie, @CookieValue("uid") String uid) {
        User user = new User();
        if (useridcookie != null) {
            //获取用户对象
            Integer userid = Integer.parseInt(useridcookie);
            user = userService.getUserByuserid(userid);
            //把用户存进session
            session.setAttribute("user", user);
        }
        //把待支付订单删除
        userRecordService.DeleteUnpaid(Integer.parseInt(uid));
        ModelAndView mv = new ModelAndView("userHome");
        //查询用户的订单并返回
        List<UserRecordFinall> userRecordFinalls = userRecordService.FindUserRecord(user.getUserid());
        mv.addObject("userRecordFinalls", userRecordFinalls);
        userRecordFinalls.forEach(System.out::println);
        return mv;
    }

    /**
     * 订单超时未处理
     *
     * @param session
     * @return
     */
    @GetMapping(value = "personal2")
    public ModelAndView UserRecordrevocation(HttpSession session) {
        ModelAndView mv = new ModelAndView("../tellogin");
        if (session.getAttribute("user") != null) {
            User user = (User) session.getAttribute("user");
            mv.setViewName("userHome");
            //查询用户的订单并返回
            List<UserRecordFinall> userRecordFinalls = userRecordService.FindUserRecord(user.getUserid());
            mv.addObject("userRecordFinalls", userRecordFinalls);
            userRecordFinalls.forEach(System.out::println);
            return mv;
        }
        return mv;
    }


}
